//
//  HomePageViewController.m
//  礼物
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "HomePageViewController.h"

@interface HomePageViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *bigScrollView;
@property(nonatomic,strong)UIScrollView *smallScrollView;
@property(nonatomic,strong)NSMutableArray *buttonArray;
@property(nonatomic,strong)NSArray *titleArray;

@property (nonatomic, strong) SelectionViewController *selectVC;
@property (nonatomic, strong) GiftViewController *giftVC;
@property (nonatomic, strong) CateViewController *cateVC;
@property (nonatomic, strong) DigitalViewController *digitalVC;
@property (nonatomic, strong) SportViewController *sportVC;
@property (nonatomic, strong) EntertainmentViewController *entertainmentVC;



@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"首页";
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    self.titleArray = [NSMutableArray arrayWithObjects:@"精选",@"礼物",@"美食",@"数码",@"运动", @"涨姿势",nil];
    self.smallScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 30)];
    self.bigScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, self.smallScrollView.frame.size.height, WIDTH, HEIGHT)];
    [self.bigScrollView setContentSize:CGSizeMake(WIDTH*self.titleArray.count, 0)];
    [self.bigScrollView setBounces:NO];
    [self.bigScrollView setPagingEnabled:YES];
    [self.bigScrollView setDelegate:self];
    [self.bigScrollView setShowsHorizontalScrollIndicator:NO];
    [self.bigScrollView setShowsVerticalScrollIndicator:NO];

    [self.view addSubview:self.bigScrollView];
        self.smallScrollView.contentSize=CGSizeMake (WIDTH/5 * self.titleArray.count, 0);
    [self.smallScrollView setBounces:NO];//滚到边缘没有反弹效果
    [self.smallScrollView setPagingEnabled:NO];//不按页翻
    self.smallScrollView.showsHorizontalScrollIndicator=NO;
    self.smallScrollView.panGestureRecognizer.delaysTouchesBegan=YES;
    self.smallScrollView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.smallScrollView];
    self.buttonArray=[NSMutableArray array];
    for (int i=0; i<self.titleArray.count; i++) {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(i*WIDTH/5, 0, WIDTH/5, 30);
        [button setTag:i];//设置标记
        [button setTitle:[self.titleArray objectAtIndex:i] forState:UIControlStateNormal];
        button.backgroundColor=[UIColor clearColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [self.smallScrollView addSubview:button];
        [self.buttonArray addObject:button];
        if (i==0) {
            [button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:215.0/255.0 blue:164.0/255.0 alpha:0.680] forState:UIControlStateNormal];
            [button.titleLabel setFont:[UIFont systemFontOfSize:18]];
        }
    }
    
    
    SelectionViewController *selectionVC=[[SelectionViewController alloc]init];
    CateViewController *cateVC=[[CateViewController alloc]init];
    DigitalViewController *digitalVC=[[DigitalViewController alloc]init];
    EntertainmentViewController *entertainmentVC=[[EntertainmentViewController alloc]init];
    GiftViewController *giftVC=[[GiftViewController alloc]init];
    SportViewController *sportVC=[[SportViewController alloc]init];
    selectionVC.view.tag=1000;
    giftVC.view.tag=1001;
    cateVC.view.tag=1002;
    digitalVC.view.tag=1003;
    sportVC.view.tag=1004;
    entertainmentVC.view.tag=1005;
    
    
    [self addChildViewController:selectionVC];
    [self addChildViewController:giftVC];
    [self addChildViewController:cateVC];
    [self addChildViewController:digitalVC];
    [self addChildViewController:sportVC];
    [self addChildViewController:entertainmentVC];
    
    selectionVC.view.frame=CGRectMake(0, 0, WIDTH, HEIGHT);
    giftVC.view.frame=CGRectMake(WIDTH, 0, WIDTH, HEIGHT);
    cateVC.view.frame=CGRectMake(WIDTH*2, 0, WIDTH, HEIGHT);
    digitalVC.view.frame=CGRectMake(3*WIDTH, 0, WIDTH, HEIGHT);
    sportVC.view.frame=CGRectMake(4*WIDTH, 0, WIDTH, HEIGHT);
    entertainmentVC.view.frame=CGRectMake(5*WIDTH, 0, WIDTH, HEIGHT);
            [self.bigScrollView addSubview:selectionVC.view];
    [self.bigScrollView addSubview:cateVC.view];
    [self.bigScrollView addSubview:digitalVC.view];
    [self.bigScrollView addSubview:entertainmentVC.view];
    [self.bigScrollView addSubview:giftVC.view];
    [self.bigScrollView addSubview:sportVC.view];
    
    
    

    self.number=0;
    
}
//点击事件
-(void)buttonClick:(UIButton *)button
{
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
    }
    [button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:215.0/255.0 blue:164.0/255.0 alpha:0.680] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [button setBackgroundColor:[UIColor whiteColor]];
    if (button.frame.origin.x==0||button.frame.origin.x==WIDTH/5||button.frame.origin.x==2*WIDTH/5) {
        [self.smallScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else if (button.frame.origin.x==(self.buttonArray.count-2)*WIDTH/5||button.frame.origin.x==(self.buttonArray.count-1)*WIDTH/5||button.frame.origin.x==(self.buttonArray.count-0)*WIDTH/5)
    {
        [self.smallScrollView setContentOffset:CGPointMake((self.buttonArray.count-5)*WIDTH/5, 0) animated:YES];
    }else
    {
        [self.smallScrollView setContentOffset:CGPointMake(WIDTH/5, 0) animated:YES];
    }
    [self.bigScrollView setContentOffset:CGPointMake(button.tag*WIDTH, 0) animated:YES];
}
//scrollView滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    
    if (self.bigScrollView==scrollView)
    {
        for (int i=0; i<self.buttonArray.count; i++)
        {
            UIButton *button1=[self.buttonArray objectAtIndex:i];
            [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button1.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [button1 setBackgroundColor:[UIColor whiteColor]];
        }
        
        UIButton *button=[self.buttonArray objectAtIndex:self.bigScrollView.contentOffset.x/WIDTH];
        [button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:215.0/255.0 blue:164.0/255.0 alpha:0.680] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:18]];
        [button setBackgroundColor:[UIColor whiteColor]];
        
        if (self.bigScrollView.contentOffset.x==0||self.bigScrollView.contentOffset.x/WIDTH==1||self.bigScrollView.contentOffset.x/WIDTH==2)
        {
            [self.smallScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }else if (button.frame.origin.x==(self.buttonArray.count-2)*WIDTH/5||button.frame.origin.x==(self.buttonArray.count-1)*WIDTH/5||button.frame.origin.x==(self.buttonArray.count-0)*WIDTH/5)
        {
            [self.smallScrollView setContentOffset:CGPointMake((self.buttonArray.count-5)*WIDTH/5, 0) animated:YES];
        }else
        {
            [self.smallScrollView setContentOffset:CGPointMake(WIDTH/5, 0) animated:YES];
        }
        
        
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
